#ifndef XMI_IMPORTER_H
#define XMI_IMPORTER_H

#include <icontainerdocument.h>

#include <QtCore/QHash>
#include <QtCore/QObject>
#include <QtCore/QUuid>

QT_BEGIN_NAMESPACE
class QUrl;
class QXmlItem;
class QXmlQuery;
class QXmlResultItems;
QT_END_NAMESPACE

namespace core
{
class IGraphicEditorItem;
}

namespace swp
{
class GraphicData;

class XmiImporter : public QObject
{
    Q_OBJECT

private:   
    class ConnectionElement
    {
    public:
        QString Id;
        QString FromId;
        QString ToId;
        QString Type;
    };
    class ModelElement
    {
    public:
        QString Name;
        QString Id;
        QString Type;
    };
    class DiagramElement
    {
    public:
        QString Shape;
        QString Id;
        QString FId;
        QString Geometry;
    };
    class Diagram
    {
    public:
        QString Name;
        QString Id;
        QString DiagramType;
        QList<QString> Elements;
    };

public:
    static XmiImporter* getInstance();
    static void destroy();

    void import(const QSharedPointer<core::IContainerDocument>& project,
        const QUrl& destination);

private:
    XmiImporter();
    ~XmiImporter();

    QString attributeValue(QXmlQuery& query, const QString& attribute,
        const QXmlItem& element) const;
    QString value(QXmlQuery& query, const QXmlItem& element) const;
    void childElement(QXmlQuery& query, const QString& name,
        const QXmlItem& element, QXmlResultItems& child) const;

    void createDocuments(
        const QSharedPointer<core::IContainerDocument>& project);
    void fillDocuments(core::IDocumentData* documentData,
        const Diagram& diagram);
    core::IGraphicEditorItem* createEditorItem(const ModelElement& mel,
        const DiagramElement& el) const;
    void createConnection(const ConnectionElement& mel,
        const DiagramElement& el, GraphicData* data) const;

    void parseGeneralizations(QXmlQuery& query, const QXmlItem& parent,
        const ModelElement& me);
    void parseAssociation(QXmlQuery& query, const QXmlItem& element,
        const ModelElement& me);
    void parseControlFlows(QXmlQuery& query, const QXmlItem& parent,
        const ModelElement& me);

signals:
    void importStarted();
    void importFinished();

private:
    QHash<QString, ConnectionElement> m_connectionElements;
    QHash<QString, ModelElement> m_modelElements;
    QHash<QString, DiagramElement> m_diagramElements;
    QHash<QString, Diagram> m_diagrams;
    QHash<QString, core::IGraphicEditorItem*> m_itemMapping;
};

} // namespace swp

#endif
